{{ $file := (.Get "file") }}
{{ $content := readFile $file }}

{{ $first_line_contains := (.Get "first_line_contains") }}
{{ $last_line_contains := (.Get "last_line_contains") }}
{{ $last_line_equals := (.Get "last_line_equals") }}

{{ $show_line := false }}

{{/*if true, first or last line was not found*/}}
{{ $first_line_found := false}}
{{ $last_line_found := false}}

{{ $padding_after := (.Get "padding_after" | default "0" | int) }}

{{ $first_line_num := 0 }}
{{ $last_line_num := 0 }}

{{ $linkFile := $file }}
{{ $repo := "watermill" }}

{{ if in $file "src-link/watermill-" }}
    {{ $repo = index (findRE "watermill-[a-z]+" $linkFile) 0 }}
    {{ $linkFile = replace $linkFile $repo "" }}
    {{ $linkFile = replace $linkFile "src-link//" "" }}
{{ else if in $linkFile "src-link/" }}
    {{ $linkFile = replace $linkFile "src-link/" "" }}
{{ else }}
    {{ $linkFile = print "docs/content/" $linkFile }}
{{ end }}

{{ $lines := slice }}

{{ range $elem_key, $elem_val := split $content "\n" }}
    {{ $line_num := (add $elem_key 1) }}

    {{ if and (not $first_line_found) (in $elem_val $first_line_contains) }}
        {{ if ne $elem_key 0 }}
            {{ $lines = $lines | append "// ..." }}
        {{ end }}

        {{ $show_line = true }}
        {{ $first_line_found = true}}
        {{ $first_line_num = $line_num }}
    {{ end }}

    {{ if $show_line }}
        {{ $lines = $lines | append $elem_val }}
    {{ end }}

    {{ if and ($first_line_found) (in $elem_val $last_line_contains) (ne $last_line_contains "") }}
        {{ $last_line_found = true }}
    {{ end }}

    {{ if and ($first_line_found) (eq $elem_val $last_line_equals) (ne $last_line_equals "") }}
        {{ $last_line_found = true }}
    {{ end }}

    {{ if and $last_line_found $show_line }}
        {{ if gt $padding_after 0 }}
            {{ $padding_after = sub $padding_after 1}}
        {{ else }}
            {{ $lines = $lines | append "// ..." }}
            {{ $show_line = false }}
            {{ $last_line_num = $line_num }}
        {{ end }}
    {{ end }}
{{ end }}

{{/* Calculate minimum indentation (common whitespace) */}}
{{ $min_indent := 999999 }}
{{ range $lines }}
    {{ if and (ne . "// ...") (ne (trim . " \t") "") }}
        {{ $leading := 0 }}
        {{ range $i, $char := split . "" }}
            {{ if or (eq $char " ") (eq $char "\t") }}
                {{ $leading = add $leading 1 }}
            {{ else }}
                {{ break }}
            {{ end }}
        {{ end }}
        {{ if lt $leading $min_indent }}
            {{ $min_indent = $leading }}
        {{ end }}
    {{ end }}
{{ end }}

{{/* Remove common leading whitespace from all lines */}}
{{ $normalized_lines := slice }}
{{ range $lines }}
    {{ if or (eq . "// ...") (eq (trim . " \t") "") }}
        {{ $normalized_lines = $normalized_lines | append . }}
    {{ else if gt $min_indent 0 }}
        {{ $normalized_lines = $normalized_lines | append (substr . $min_indent) }}
    {{ else }}
        {{ $normalized_lines = $normalized_lines | append . }}
    {{ end }}
{{ end }}

<div class="expressive-code">
    <figure class="frame not-content">
        {{ transform.Highlight (delimit $normalized_lines "\n" | safeHTML) (.Get "type" | default "go") }}
    </figure>
</div>

<small class="smaller">Full source: [github.com/ThreeDotsLabs/{{ $repo }}/{{ $linkFile }}](https://github.com/ThreeDotsLabs/{{ $repo }}/tree/master/{{ $linkFile }}{{ if ne $first_line_num 0 }}#L{{ $first_line_num }}{{ end }})</small>

{{if not $first_line_found }}
    {{ errorf "`first_line_contains` %s not found in %s snippet" $first_line_contains $file }}
{{end}}

{{if and (not $last_line_found) (ne $last_line_contains "") }}
    {{ errorf "`last_line_contains` %s not found in %s snippet" $last_line_contains $file }}
{{end}}
